package com.cloud.pole.param.controller;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.pole.api.param.PicklistApi;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.rest.BaseController;
import com.cloud.pole.param.biz.PickListBiz;
import com.cloud.pole.param.model.entity.Picklist;
import com.cloud.pole.param.service.ParamPickListService;
import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("pickList/front")
@Slf4j
public class ParamPickListController extends BaseController<PickListBiz,Picklist> {

	@Autowired
	ParamPickListService paramPickListService;
	
	@ResponseBody
	@RequestMapping("/getPickList")
    public ApiRestResponse<List<PicklistApi>> getPickList(String picklistCode){   	
    	try {
    		ParamException.err(StringUtils.isBlank(picklistCode), "picklistCode不能为空");			
			List<Picklist> srcList = paramPickListService.getPickList(picklistCode);
			List<PicklistApi> descList = new ArrayList<PicklistApi>();
			if(srcList != null) {
				for(Picklist picklist : srcList) {
					PicklistApi picklistVo = new PicklistApi();
					BeanUtils.copyProperties(picklist, picklistVo);
					descList.add(picklistVo);
				}
			}
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, descList);
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
}
